[#ftl]
[#include "MetadataAware.ftl"/]
[@define "agile._MetadataEditor" "agile._MetadataAware"]
	postMixInProperties:function(){
		if(this.templateString){			
			this.templateString = lang.replace(this.templateString,agile.i18nValues(this.templateString,this));			
		}
	}	
[/@]

/*composite property editors to be serialized as corresponding style attributes, the name, htmlName, propertyNames is not required*/
[@define "agile._XMetadataEditor","agile._MetadataEditor"]
 	constructor:function(){
 		this.propertyEditors = {};
 	}
	/*lookup property editors*/
 	,init:function(){
// 		//console.log(this.declaredClass,"[agile._XMetadataEditor]onStartup");
		query(".peditor",this.domNode).forEach(function(node,index){
			var editor = registry.byNode(node);						
			this.propertyEditors[editor.name] = editor;
			agile.stopKeyEvent(editor);
		},this);
		
		this.subscribe("agile.widget.changed.styles",function(widget,name,value){
			name = name.replace("style.","");
			if(this.propertyEditors[name]){
				this.propertyEditors[name].reset1("value",value);
			}
		});
 	}
 	/*initialize the state of the property editor according to the style constraints defined in the current selected widget*/
 	,initState:function(context){				
		var widget = context.getCurrentWidget();
//		//console.log(this.declaredClass,"[_XMetadataEditor]initState...currentWidget");
		if(widget){
			var constraint, readOnly;
//			console.log(widget.declaredClass,"###styleConstraints=",widget.styleConstraints,"this.propertyEditors=",this.propertyEditors);
			for(var name in this.propertyEditors){				
				constraint = widget.styleConstraints[this.propertyEditors[name].htmlName];
				//console.log(widget.declaredClass,name,"constraint=",constraint,agile.MetadataConstrain);
				if(constraint){
					readOnly = constraint.readOnly(widget);
				}else{
					readOnly = false;
				}
				this.propertyEditors[name].set("disabled",readOnly);
//				if(readOnly){
////					style.set(this.propertyEditors[name].domNode,{backgroundColor:"red"});
////					domClass.add(this.propertyEditors[name].domNode,"dijitPlaceHolder");
//				}else{
////					domClass.remove(this.propertyEditors[name].domNode,"dijitPlaceHolder");
//				}
//				style.set(this.propertyEditors[name].domNode,{backgroundColor:"red"});
//				//console.log(this.declaredClass,"[_XMetadataEditor]initState...name=",name," constraint=",constraint);
			}		
			this.initCustomState(widget);
		}
	}
 	,get:function(name){
 		if(name == "value"){
 			var value = null;
 			if(this.isValid()){
 				value = {};
	 			for(var e in this.propertyEditors){
	 				value[this.propertyEditors[e].htmlName] = this.propertyEditors[e].get("value");
	 			}
 			}
 			return value;
 		}
 		return this.inherited(arguments);
 	}
 	,set:function(name,value){
// 		console.log(this.declaredClass,"[_XMetadataEditor] set name=",name,",value=",value);
 		if(name == "value"){
 			for(var e in this.propertyEditors){
 				this.propertyEditors[e].reset1("value",value[this.propertyEditors[e].htmlName]);
 			}		
 		}else if(name == "disabled" || name == "readOnly" || name == "context"){
 			for(var e in this.propertyEditors){
 				this.propertyEditors[e].set(name,value);
// 				if(name == "disabled" || name == "readOnly")
// 				domClass.add(this.propertyEditors[e].domNode,"dijitPlaceHolder");
 			} 			
 		}else if(name == "context"){ 			
 			this.context = value;
 		}else{
 			this.inherited(arguments);
 		}
 	}
 	,reset1:function(name,value){
 		if(name == "value"){
 			for(var e in this.propertyEditors){
 				this.propertyEditors[e].reset1(name,value[this.propertyEditors[e].htmlName]);
 			}
 		}else if(name == "context"){ 			
 			this.context = value;
 		}else{
 			this.inherited(arguments);
 		}
 	}
 	,isValid:function(){
 		return true;
 	}
[/@]

[#--/*composite property editors to be serialized as a single html style attribute, the name, htmlName, propertyNames must be defined*/--]
[#--
[@define "agile._CompositeMetadataEditor","agile._XMetadataEditor"]
	name:"",htmlName:"",propertyNames:null,valueType:"text"
	 ,init:function(){	
		this.propertyEditors = {};
		query(".editor",this.domNode).forEach(function(node){			
			var editor = registry.byNode(node);
			this.propertyEditors[editor.htmlName] = editor;
			editor.on("change",lang.hitch(this,function(value){
				this.onChange(this.get("value"),this.name,this.htmlName);
			}));
		},this);		
	}	
	,get:function(name){
		if(name == "value"){
			if(this.valueType == "text"){
				var value = "";
				if(this.isValid()){
					for(var i = 0 ; i < this.propertyNames.length; i++){
						value += this.propertyEditors[this.propertyNames[i]].get("value") +" ";
					}
				}
				return string.trim(value.replace(/null/g,""));
			}else{
				var value = {};
				if(this.isValid()){
					for(var i = 0 ; i < this.propertyNames.length; i++){
						value[this.propertyNames[i]]= this.propertyEditors[this.propertyNames[i]].get("value");
					}
				}
				return value;
			}
		}
		return this.inherited(arguments);
	}
	,set:function(name,value){
		if(name == "value"){			
			if(value && value[this.htmlName]){
				if(this.valueType == "text"){
					var v = value[this.htmlName].split(" ");
					for(var i = 0 ; i < this.propertyNames.length; i++){
						this.propertyEditors[this.propertyNames[i]].reset1("value",v[i]?v[i]:"");
					}
				}else{
					var v = value[this.htmlName];
					for(var i = 0 ; i < this.propertyNames.length; i++){
						this.propertyEditors[this.propertyNames[i]].reset1("value",v[this.propertyNames[i]]);
					}
				}
			}else{
				for(var i = 0 ; i < this.propertyNames.length; i++){
					this.propertyEditors[this.propertyNames[i]].reset1("value","");
				}
			}
		}else if(name == "disabled" || name == "readOnly"){
			for(var i = 0 ; i < this.propertyNames.length; i++){
				this.propertyEditors[this.propertyNames[i]].set(name,value);
			}
 		}else if(name == "context"){ 			
 			this.context = value;
 		}else{
			this.inherited(arguments);
		}
	}
	,reset1:function(name,value){
		if(name == "value"){
			if(value && value[this.htmlName]){
				if(this.valueType == "text"){
					var v = value[this.htmlName].split(" ");
					for(var i = 0 ; i < this.propertyNames.length; i++){
						this.propertyEditors[this.propertyNames[i]].reset1("value",v[i]?v[i]:"");
					}
				}else{
					var v = value[this.htmlName];
					for(var i = 0 ; i < this.propertyNames.length; i++){
						this.propertyEditors[this.propertyNames[i]].reset1("value",v[this.propertyNames[i]]);
					}
				}
			}else{
				for(var i = 0 ; i < this.propertyNames.length; i++){
					this.propertyEditors[this.propertyNames[i]].reset1("value","");
				}
			}
		}else if(name == "disabled" || name == "readOnly"){
			for(var i = 0 ; i < this.propertyNames.length; i++){
				this.propertyEditors[this.propertyNames[i]].set(name,value);
			}
 		}else if(name == "context"){
 			this.context = value;
 		}else{
			this.inherited(arguments);
		}
	}
[/@]
--]